﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntityObjects;
using DAL_V1.App_Code.DBBroker;

namespace BAL.Controller
{
    public class MakeRequisitionControl
    {
        private RequisitionDBBroker requisitionDBBroker;
        private CatalogueDBBroker catalogueDBBroker;
        private EmployeeDBBroker employeeDBBroker;
                     
        public MakeRequisitionControl()
        {
            requisitionDBBroker = RequisitionDBBroker.getRequisitionDBBroker();
            catalogueDBBroker = CatalogueDBBroker.getCatalogueDBBroker();
            employeeDBBroker = EmployeeDBBroker.getEmployeeDBBroker();
        }

        private int insertRequisition(Requisition insertRequisition)
        {
            //first call dbbroker method to save requisition           
            int count= requisitionDBBroker.addRequisition(insertRequisition);                           
            return count;
        }

        public Requisition makeRequisition(Employee emp)
        {
            Requisition req=null;  
            if(!IsAlreadyAppliedByEmp(emp))
            {
                req = new Requisition();
                req.DeptId=emp.DeptId;
                req.EmpId=emp.EmpId;
                req.EmpName=emp.EmpName;
                req.RequestedDate=DateTime.Now.ToString();
                req.Status="Applied";
                req.ReqDetails_list = new List<RequisitionDetails>();
            }

            return req;
        }

        public string selectSubmit(Requisition insertRequisition,Employee emp)
        {    
            string result="";
            if (emp.RoleId == 1)
            {
                insertRequisition.Status = "Approved";                
                result="Your requisition is already approved! ";
            }

            this.insertRequisition(insertRequisition);

            string deptId = insertRequisition.DeptId;            
            DateTime requestedDate = DateTime.Parse(insertRequisition.RequestedDate);
            int newReqFormId = requisitionDBBroker.getNewSerialNo();
            result += String.Format("Your new Requisition Number is {0}/{1:000}/{2:yy}", deptId, newReqFormId, requestedDate);

                //send notification mail to Dept Head
                if (emp.RoleId != 1)
                {
                    sendEmailToDeptHead(insertRequisition);
                }                        
            return result;
        }

        private void sendEmailToDeptHead(Requisition newRequisition)
        {
            List<Requisition> req = requisitionDBBroker.getRequisitionByDeptIdAndAppliedStatus(newRequisition.DeptId);
            if ((req.Count % 10) == 0)
            {
                Employee deptHead = employeeDBBroker.getEmployeebyDeptIdAndRole(newRequisition.DeptId, 1);
                string mailTo = deptHead.Email;
                Email email = new Email(req.Count, mailTo);
                email.createMail();
            }
        }

        public List<Category> getAllCategory()
        {
            return catalogueDBBroker.selectAllCategory();
        }

        public List<Catalogue> getCatalogueByCategoryId(int categoryId)
        {
            return catalogueDBBroker.selectCatalogueByCategoryId(categoryId);
        }

        public List<Catalogue> getAllCatalogue()
        {
            return catalogueDBBroker.getCatalogueForClerk();
        }

        private bool IsAlreadyAppliedByEmp(Employee emp)
        {
            bool result = false;
            if (requisitionDBBroker.getRequsitionByEmpIdAndAppliedStatus(emp.EmpId) != null)
            {
                result = true;
            }
            return result;
        }
    }
}
